package com.weitu.observation.observationMonthReport.service.impl;

import com.weitu.observation.observationMonthReport.entity.ObservationMonthRepData;
import com.weitu.observation.observationMonthReport.entity.ObservationMonthReport;
import com.weitu.observation.observationMonthReport.mapper.ObservationMonthReportMapper;
import com.weitu.observation.observationMonthReport.service.ObservationMonthReportService;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/1.
 */
@Service
public class ObservationMonthReportServiceImpl implements ObservationMonthReportService{

    private final ObservationMonthReportMapper observationMonthReportMapper;
    private final DepartmentMapper departmentMapper;

    @Autowired
    public ObservationMonthReportServiceImpl(ObservationMonthReportMapper observationMonthReportMapper,DepartmentMapper departmentMapper){
        this.observationMonthReportMapper=observationMonthReportMapper;
        this.departmentMapper=departmentMapper;
    }
    private void toDeptIds(Map<String, Object> map){
        String deptId=map.get("deptId").toString();
        List<String> deptIds=new ArrayList<String>();
        addDeptId(deptIds,deptId);
        map.put("deptIds",deptIds);
    }
    private void addDeptId(List<String> deptIds,String deptId){
        deptIds.add(deptId);
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(deptId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                addDeptId(deptIds,id);
            }
    }
    @Override
    public List<ObservationMonthReport> queryList(Map<String, Object> map) {
        toDeptIds(map);
        return this.observationMonthReportMapper.queryList(map);
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        toDeptIds(map);
        return this.observationMonthReportMapper.queryCount(map);
    }

    //废弃的
    @Override
    public List<ObservationMonthRepData> queryDataList(String deptId,String date) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("date",date);
        map.put("deptId",deptId);
        List<ObservationMonthRepData> observationMonthRepDataList=new ArrayList<ObservationMonthRepData>();
        ObservationMonthRepData monthRepData=this.observationMonthReportMapper.queryDataList(map);
        if(monthRepData!=null)
        observationMonthRepDataList.add(monthRepData);
        getReportData(observationMonthRepDataList,deptId,map);
        return observationMonthRepDataList;
    }
    //废弃的
    private void getReportData(List<ObservationMonthRepData> observationMonthRepDataList, String parentId,Map<String,Object> map) {
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(parentId);
        if(parentDepartmentList.size()>0)
        for (Department department : parentDepartmentList) {
            String id = department.getId();
            map.put("deptId",id);
            ObservationMonthRepData monthRepData=this.observationMonthReportMapper.queryDataList(map);
            if(monthRepData!=null)
            observationMonthRepDataList.add(monthRepData);
            getReportData(observationMonthRepDataList,id,map);
        }
    }

    @Override
    public List<ObservationMonthRepData> queryDataListNext(String deptId, String date) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("date",date);
        map.put("deptId",deptId);
        List<ObservationMonthRepData> observationMonthRepDataList=new ArrayList<ObservationMonthRepData>();
        ObservationMonthRepData monthRepData=getReportDataNext(deptId,map);

        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(deptId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                ObservationMonthRepData repData=getReportDataNext(id,map);
                observationMonthRepDataList.add(repData);
            }
        observationMonthRepDataList.add(monthRepData);
        return observationMonthRepDataList;
    }

    private void combine(ObservationMonthRepData one,ObservationMonthRepData two){
        one.setAllCount((one.getAllCount()==null?0:one.getAllCount())+(two.getAllCount()==null?0:two.getAllCount()));
        one.setActCount((one.getActCount()==null?0:one.getActCount())+(two.getActCount()==null?0:two.getActCount()));
        one.setStateCount((one.getStateCount()==null?0:one.getStateCount())+(two.getStateCount()==null?0:two.getStateCount()));
        one.setSafeActCount((one.getSafeActCount()==null?0:one.getSafeActCount())+(two.getSafeActCount()==null?0:two.getSafeActCount()));
        one.setObsNums((one.getObsNums()==null?0:one.getObsNums())+(two.getObsNums()==null?0:two.getObsNums()));
        one.setObsProblemNums((one.getObsProblemNums()==null?0:one.getObsProblemNums())+(two.getObsProblemNums()==null?0:two.getObsProblemNums()));
        one.setObsUnSafeNums((one.getObsUnSafeNums()==null?0:one.getObsUnSafeNums())+(two.getObsUnSafeNums()==null?0:two.getObsUnSafeNums()));
    }

    private ObservationMonthRepData getReportDataNext(String parentId,Map<String,Object> map) {
        ObservationMonthRepData repData=this.observationMonthReportMapper.queryDataListNext(map);
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(parentId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                ObservationMonthRepData monthRepData=getReportDataNext(id,map);
                combine(repData,monthRepData);
            }

        return repData;
    }

    @Override
    @Transactional
    public int insertReportAndData(ObservationMonthReport monthReport, List<ObservationMonthRepData> monthRepDatas) {
        if(monthRepDatas.size()>0)
        this.observationMonthReportMapper.insertRepData(monthRepDatas);
        return this.observationMonthReportMapper.insertReport(monthReport);
    }

    @Override
    public ObservationMonthReport queryById(String id) {
        return this.observationMonthReportMapper.queryById(id);
    }

    @Override
    public List<ObservationMonthRepData> queryByReportId(String id) {
        return this.observationMonthReportMapper.queryByReportId(id);
    }

    @Override
    public int selectJudgement(Map<String,Object> map) {
        return this.observationMonthReportMapper.selectJudgement(map);
    }

    @Override
    @Transactional
    public int updateMontReport(ObservationMonthReport report,List<ObservationMonthRepData> monthRepDataList) {
        this.observationMonthReportMapper.deleteReportData(report.getId());
        if(monthRepDataList.size()>0)
            this.observationMonthReportMapper.insertRepData(monthRepDataList);
        return this.observationMonthReportMapper.updateMontReport(report);
    }

    @Override
    public int deleteReport(String id) {
        return this.observationMonthReportMapper.deleteReport(id);
    }
}
